package com.mvp.lt.airlineview.utils;

import com.mvp.lt.airlineview.data.constant.NetUrls;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.ObservableResponse;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * $activityName
 *
 * @author LiuTao
 * @date 2019/3/19/019
 */


public class SmsCodeUtils {
    public interface SmsListener {
        void showDialogLoading();

        void onSubScribe(Disposable d);

        void dissDialogmissLoad();
    }

    /**
     * 注册时的验证码
     * @param jsonObject
     * @param smsListener
     */
    public static void getSMSCode(JSONObject jsonObject, SmsListener smsListener) {
        OkGo.<String>post(NetUrls.URL_REGIST_SEDNSMS_CODE)
                .upJson(jsonObject)
                .converter(new StringConvert())
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        smsListener.showDialogLoading();
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        smsListener.onSubScribe(d);
                    }

                    @Override
                    public void onNext(Response<String> stringResponse) {

                        if (stringResponse.isSuccessful()) {
                            String s = stringResponse.body();
                            LogUtil.e("获取验证码", s);
                            try {
                                JSONObject jsonObjectr = new JSONObject(s);
                                String code = jsonObjectr.optString("code");
                                if (code.equals("1")) {
                                    String msg_customer = jsonObjectr.optString("msg_customer");
                                    ToastUtils.showToast(msg_customer);
                                } else if ("0".equals(code)) {
                                    String msg_customer = jsonObjectr.optString("msg_customer");
                                    ToastUtils.showToast(msg_customer);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            ToastUtils.showToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        smsListener.dissDialogmissLoad();
                        ToastUtils.showToast("请求失败");
                    }

                    @Override
                    public void onComplete() {
                        smsListener.dissDialogmissLoad();
                    }
                });
    }

    /**
     * 其他时候用的验证码
     * @param jsonObject
     * @param smsListener
     */
    public static void getLoginSMSCode(JSONObject jsonObject, SmsListener smsListener) {
        OkGo.<String>post(NetUrls.URL_LOGIN_SEDNSMS_CODE)
                .upJson(jsonObject)
                .converter(new StringConvert())
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        smsListener.showDialogLoading();
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        smsListener.onSubScribe(d);
                    }

                    @Override
                    public void onNext(Response<String> stringResponse) {

                        if (stringResponse.isSuccessful()) {
                            String s = stringResponse.body();
                            LogUtil.e("获取验证码", s);
                            try {
                                JSONObject jsonObjectr = new JSONObject(s);
                                String code = jsonObjectr.optString("code");
                                if (code.equals("1")) {
                                    String msg_customer = jsonObjectr.optString("msg_customer");
                                    ToastUtils.showToast(msg_customer);
                                } else if ("0".equals(code)) {
                                    String msg_customer = jsonObjectr.optString("msg_customer");
                                    ToastUtils.showToast(msg_customer);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            ToastUtils.showToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        smsListener.dissDialogmissLoad();
                        ToastUtils.showToast("请求失败");
                    }

                    @Override
                    public void onComplete() {
                        smsListener.dissDialogmissLoad();
                    }
                });
    }
}
